package com.dotstone.chipism.adapter;

import java.util.List;

import com.dotstone.chipism.R;
import com.dotstone.chipism.bean.Key;
import com.dotstone.chipism.util.KeyManager;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

/*************************************************************************
 * 公 司： 深圳市高金科技有限公司 作 者： 深圳市高金科技有限公司 文件名称：KeySelectAdapter.java 内容摘要：选择按键的适配器 日
 * 期： 2016-11-28
 ************************************************************************/
public class KeySelectAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private List<Key> mKeyList;
	private Context context;
	private int imageResId;
	private int stringResId;
	private int bgDrawableResId;

	public KeySelectAdapter(Context context, List<Key> mKeyList) {
		this.context = context;
		this.mKeyList = mKeyList;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {

		return mKeyList.size();
	}

	@Override
	public Object getItem(int position) {

		return mKeyList.get(position);
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		convertView = inflater.inflate(R.layout.item_select_key, null);
		Key key = mKeyList.get(position);
		TextView nameTv = (TextView) convertView.findViewById(R.id.key_name_tv);
		ImageView keyImg = (ImageView) convertView.findViewById(R.id.key_img);
		ImageView selectedImg = (ImageView) convertView
				.findViewById(R.id.selected_img);
		TextView keyTv = (TextView) convertView.findViewById(R.id.key_tv);
		FrameLayout keyIconLayout = (FrameLayout) convertView
				.findViewById(R.id.key_icon_layout);
		bgDrawableResId = KeyManager.getInstance().getBackGroundResId(
				key.getKeyNum());
		nameTv.setText(key.getName());
		if (key.getKeyNum() == 800 || key.getKeyNum() == 4000
				|| key.getKeyNum() == 818 || key.getKeyNum() == 819
				|| key.getKeyNum() == 820 || key.getKeyNum() == 821) {
			keyIconLayout.setBackgroundColor(Color.WHITE);
		}

		if (key.getContentType() == 0) {
			imageResId = KeyManager.getInstance().getDrawableResId(
					key.getKeyNum());
			keyTv.setVisibility(View.GONE);
			keyImg.setImageResource(imageResId);
		} else {
			stringResId = KeyManager.getInstance().getStringResId(
					key.getKeyNum());
			keyImg.setVisibility(View.GONE);
			keyTv.setText(stringResId);
		}
		if (!key.getIsSelected()) {
			selectedImg.setVisibility(View.GONE);
		} else {
			selectedImg.setVisibility(View.VISIBLE);
		}

		return convertView;
	}

}